package com.app.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.app.draft.service.DraftService;
import com.app.sso.client.servlet.UserBean;
import com.app.util.Constant;

@Controller
public class UserController {
	@Resource(name="draftService")
	private DraftService draftService;
	@RequestMapping("/userindex")
	public void getUser(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HttpSession se = request.getSession();
		UserBean b = (UserBean)se.getAttribute("CNNAME");
		if(b==null){
			response.sendRedirect("caoanList.html");
		}else{
			response.sendRedirect("caoanAdminList.html");
		}
		/*response.setContentType("text/html;charset=UTF-8"); 
		Map m = new HashMap();
		m.put("username", "zhangsan");
		response.getWriter().write(JSON.toJSONStringWithDateFormat(m, "yyyy-MM-dd hh:mm:ss"));*/
	}
	
	@RequestMapping("/fileUpload")
	public void fileUpload(HttpServletRequest request,HttpServletResponse response){
		try {
			HttpSession se = request.getSession();
			response.setContentType("text/html;charset=UTF-8"); 
			String ca_name = request.getParameter("ca_name");
			String department = request.getParameter("department");
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			Map map = new HashMap();
			map.put("title", ca_name);
			map.put("startDate", startDate);
			map.put("endDate", endDate);
			map.put("departid", department);
			
			String file = "";
			CommonsMultipartResolver cmr = new CommonsMultipartResolver(request.getSession().getServletContext());
			if(cmr.isMultipart(request)){
				MultipartHttpServletRequest mrequest = (MultipartHttpServletRequest) request;
				Iterator it = mrequest.getFileNames();
				while(it.hasNext()){
					MultipartFile mf = mrequest.getFile(it.next().toString());
					String name = mf.getOriginalFilename();
					file = "zqyj/admin/pubDoc/"+name;
					map.put("pubdoc", file);
					mf.transferTo(new File(Constant.WebRoot+file));
				}
			}
			draftService.saveDraft(map, new File(Constant.WebRoot + file));
			response.getWriter().write("{success:true,msg:'上传成功'}");
		} catch (Exception e) {
			try {
				response.getWriter().write("{failure:true,msg:'上传失败'}");
			} catch (Exception e2) {
			}
		}
	}
	@RequestMapping("/user/fileDownload")
	public void fileDownload(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String agent = request.getHeader("User-Agent");
		String filename = "中文";
		if(agent!=null){
			if(agent.toLowerCase().indexOf("firefox")!=-1){
				filename = new String(filename.getBytes(),"iso8859-1");
			}else{
				filename = URLEncoder.encode(filename,"utf8");
			}
		}else{
			filename = URLEncoder.encode(filename,"utf8");
		}
		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename="+filename+".txt");  
        response.setContentType("application/octet-stream; charset=utf-8");  
        OutputStream out = response.getOutputStream();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("e:/route.txt"));
        byte[] b = new byte[4096];
        int r;
        while((r=bis.read(b))!=-1){
        	out.write(b, 0, r);
        }
        bis.close();
        out.close();
	}
}
